# -*- coding: utf-8 -*-

import os

from django.utils.unittest import skipUnless
from django.conf import settings
from django.test import TestCase

from financial_daily.stocks_category_info_crawler import StocksCategoryInFoCrawler
from financial_daily.utils import WebUtils
from basic_info.tasks import  crawl_stocks_info, write_stocks_info_to_db
from basic_info.models import Industry, Stock, StockIndustryMap


class BasicInfoTest(TestCase):
     
    #@skipUnless(os.environ['DJANGO_SETTINGS_MODULE'].endswith('test'), "Only test this on a test server.")   
    def test_crawl_stocks_info(self):
       
        # crawl sina stocks info         
        crawler = StocksCategoryInFoCrawler()
        config_file = settings.CRAWL_STOCKS_INFO_FILES['sina']
        flag, industry_args, location_args, concept_args = crawler.parse_config_file(config_file) 
         
        if industry_args: 
            file_name = settings.CRAWL_STOCKS_INFO_FILE_NAME['sina'] + \
                        settings.CRAWL_STOCKS_INFO_CATEGORY['industry']
            crawler.crawl_data(industry_args, file_name, flag)
        
            # insert stocks info into the test_db
            stock,created = Stock.objects.get_or_create(code=u'600011',
                            short_name=u'包钢股份',
                            full_name=u'内蒙古包钢钢联股份有限公司',
                            pinyin_name=u'bggf'
                            )
            
            # insert crawl result into test_db
            file_path = os.path.join(settings.CRAWL_STOCKS_INFO_FILE_PATH, file_name)
            lines = crawler.read_file(file_path)  
            for line in lines:
                line = line.strip()
                entry = line.split(settings.CRAWL_COLUMNS_DELIMITER)
                stock_category = entry[0]
                stock_code = entry[1] 
                industry,created = Industry.objects.get_or_create(name=stock_category)

            industry = Industry.objects.get(name=u'电力行业')
            stock = Stock.objects.get(code='600011')
            stock_map, created = StockIndustryMap.objects.get_or_create(industry=industry, stock=stock)
            self.assertTrue(created)
        
    def test_write_stocks_info_to_db(self):
       
        # insert stock info into the test_db   
        stock,created = Stock.objects.get_or_create(code=u'600011',
                        short_name=u'包钢股份',
                        full_name=u'内蒙古包钢钢联股份有限公司',
                        pinyin_name=u'bggf'
                        )
        self.assertTrue(created)

        # insert industry info into the test_db
        industry,created = Industry.objects.get_or_create(name=u'制造业')
        self.assertTrue(created)
        
        stock = Stock.objects.get(code=u'600011')

        # insert stock_industry_map
        stock_industry_map, created = StockIndustryMap.objects.get_or_create(industry=industry, stock=stock)
        self.assertTrue(created)

        stock = Stock.objects.get(code=u'600011')
        stock.csrc_industry = u'制造业'
        stock.csrc_sub_industry = u'仪器仪表制造业'
        stock.save()
        self.assertEqual(stock.csrc_industry,u'制造业')
        self.assertEqual(stock.csrc_sub_industry,u'仪器仪表制造业')

